<?php


namespace common\behavior;

use common\service\OptionsService;
use common\service\TaskWorkOrderService;

/**
 * 通知工单用户新增成员 工单需要处理
 */
class TaskWorkOrderUserAddedNotifyBehavior
{

    public function run($data)
    {
        $smsStatus = OptionsService::getInstance()->getOptionsConfigItemData('task_work_order_config', 'sms_switch', 'off') === 'on';
        $userAddSmsStatus = OptionsService::getInstance()->getOptionsConfigItemData('task_work_order_config', 'user_add_sms_switch', 'off') === 'on';
        if (!$smsStatus) {
            APP_DEBUG && trace('task_work_order_config.sms_switch is off or null', 'DEBUG');
            return;
        }
        if (!$userAddSmsStatus) {
            APP_DEBUG && trace('task_work_order_config.user_add_sms_switch is off or null', 'DEBUG');
            return;
        }

        $taskWorkOrderService = TaskWorkOrderService::getInstance();
        if (!empty($data['join_type']) && $data['join_type'] === 'self') {
            // 自己加入的 不发送短信通知
            return;
        }
        try {
            $userIdList = array_column($data['add'], 'user_id');
            $workOrderId = $data['work_order_id'];
            $taskWorkOrderService->notifyTaskWorkOrderUserAdded($workOrderId, $userIdList);
        } catch (\Throwable $e) {
            trace('发送短信通知失败' . PHP_EOL . format_exception_info($e), 'ERR');
        }
    }
}
